<?php


namespace Tools\Utils;


class Email
{

    /**
     * 邮件发送函数
     */
    public static function sendMail($to, $subject, $content, $path = array())
    {
        Vendor('PHPMailer.PHPMailerAutoload');
        $mail = new PHPMailer ();
        // 实例化
        $mail->IsSMTP();
        // 启用SMTP
        $mail->Host = config('MAIL_HOST');
        // smtp服务器的名称（这里以126邮箱为例）
        $mail->SMTPAuth = config('MAIL_SMTPAUTH');
        // 启用smtp认证
        $MAIL_FROM = config('MAIL_FROM');
        $key = time() % count($MAIL_FROM);
        $sender = $MAIL_FROM[$key];
        //获取邮箱账号
        $mail->Username = $sender;
        // 你的邮箱名
        $PASSWORD = config('MAIL_PASSWORD');
        $mail->Password = $PASSWORD[$key];
        // 邮箱密码
        $mail->From = $sender;
        // 发件人地址（也就是你的邮箱地址）
        $mail->FromName = config('MAIL_FROMNAME');
        // 发件人姓名
        $mail->addAddress($to, config('MAIL_FROMNAME'));
        $mail->WordWrap = 50;
        //端口配置
        $mail->SMTPSecure = config('MAIL_SMTPSecure');
        $mail->Port = config('MAIL_PORT');
        // 设置每行字符长度
        $mail->IsHTML(config('MAIL_ISHTML'));
        // 是否HTML格式邮件
        $mail->CharSet = config('MAIL_CHARSET');
        // 设置邮件编码
        $mail->Subject = $subject;
        // 邮件主题
        $mail->Body = $content;

        if ($path) {
            foreach ($path as $value) {
                if ($value['path'] && $value['name']) {
                    $res = $mail->AddAttachment($value['path'], $value['name']); // 添加附件,并指定名称
                }
            }
        }


        // 邮件内容
        $mail->AltBody = "This is the body in plain text for non-HTML mail clients";

        // 邮件正文不支持HTML的备用显示
        if (!$mail->Send()) {
            //		echo "Message could not be sent. <p>";
            $res = "Mailer Error: " . $mail->ErrorInfo . "_port:" . $mail->Port . "_sendemail:{$sender}___{$mail->Username}";
            //		exit ();
        } else {
            $res = TRUE;
        }

        M('system_email_content')->add([
            'to' => $to,
            'subject' => $subject,
            'content' => htmlspecialchars($content),
            'sender' => $sender,
            'res' => $res,
            'create_time' => date("Y-m-d H:i:s")
        ]);


        return $res;
    }

    public static function checkEmail($email)
    {
        // Create the syntactical validation regular expression
        $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

        // Presume that the email is invalid
        $valid = 0;

        // Validate the syntax
        if (preg_match($regexp, $email)) {
            list($username, $domaintld) = explode("@", $email);
            // Validate the domain
            if (getmxrr($domaintld, $mxrecords))
                $valid = 1;
        } else {
            $valid = 0;
        }

        return $valid;

    }
}
